package com.brain.core.configuracion;


import com.brain.core.exception.DAOException;
import com.brain.core.manager.RecursosManager;
import java.sql.SQLException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WebServletContextListener implements ServletContextListener {

    private static WebApplicationContext springContext;
    

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        springContext = null;
    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            springContext = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
            
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.getViewRoot().setLocale(new Locale("es_PE"));
            fc.getApplication().setDefaultLocale(FacesContext.getCurrentInstance().getViewRoot().getLocale());                
            System.out.println("Entrando ....");
            RecursosManager recursosManager = (RecursosManager) getApplicationContext().getBean("recursosManager");
            recursosManager.cargarLogger();
            recursosManager.cargarInformacionBaseDatos();
            
            try {
                recursosManager.cargarParametrosGenerales();
                //ApplicationHelper.cargarParametrosGenerales();
            } catch (DAOException ex) {
                //Logger.getLogger(WebServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (NamingException ex) {
            
        } catch (SQLException ex) {
            
        }
    }

    public static ApplicationContext getApplicationContext() {
        return springContext;
    }
}
